﻿using FrameWork.Core.Enum;
using FrameWork.Core.Adapter;
using FrameWork.Core.Infrastructure;
using FrameWork.Core.Adapter.Device;

namespace FrameWork.Services
{
    public class AdapterManager
    {
        private static SocketManager socketManager;
        /// <summary>
        /// 获取适配器
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="adapterName"></param>
        /// <returns></returns>
        public static IDeviceAdapter GetAdapter(string adapterName)
        {
            return EngineContext.Current.Resolve<IDeviceAdapter>(adapterName);
        }
        /// <summary>
        /// 获取适配器
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="adapterName"></param>
        /// <returns></returns>
        public static T GetAdapter<T>(string adapterName) where T : IDeviceAdapter
        {
            var adapter = EngineContext.Current.Resolve<IDeviceAdapter>(adapterName);
            return adapter == null ? default : (T)adapter;
        }
        /// <summary>
        /// 发送数据
        /// </summary>
        /// <param name="baseDevice"></param>
        /// <param name="byteData"></param>
        /// <param name="priority"></param>
        public static void AsyncSend(BaseDevice baseDevice, byte[] byteData, bool IgnoreLock = false, int priority = 100)
        {
            if (byteData.Length < 15)
            {
                //一个完整的包最简单结构： 包头(13)+包尾(2)
                //包长度小于15 认为不是一个完整的包结构--下面使用到了 命令Command 所以在这里简单判断长度
                return;
            }
            if (socketManager == null)
            {
                socketManager = new SocketManager();
                socketManager.Start();
            }
            int command = (byteData[9] << 8) + byteData[8];
            socketManager.AddByteData(new SocketManager.Package()
            {
                Priority = priority,
                Data = byteData,
                IgnoreLock = IgnoreLock,
                BaseDevice = baseDevice,
                //命令
                Command = (byteData[9] << 8) + byteData[8],
                CommandType = EnumCommandType.HACTS,
            });
        }
        /// <summary>
        /// 发送GGS命令
        /// </summary>
        /// <param name="baseDevice"></param>
        /// <param name="byteData"></param>
        /// <param name="IgnoreLock"></param>
        /// <param name="priority"></param>
        public static void AsyncSendToGGS(BaseDevice baseDevice, byte[] byteData, bool IgnoreLock = false, int priority = 100)
        {
            if (socketManager == null)
            {
                socketManager = new SocketManager();
                socketManager.Start();
            }
            socketManager.AddByteData(new SocketManager.Package()
            {
                Priority = priority,
                Data = byteData,
                IgnoreLock = IgnoreLock,
                BaseDevice = baseDevice,
                Command = byteData[6],
                CommandType = EnumCommandType.GGS,
            });
        }
        /// <summary>
        /// 释放信号量
        /// </summary>
        /// <param name="baseDevice"></param>
        public static void Set(BaseDevice baseDevice)
        {
            if (socketManager == null)
            {
                socketManager = new SocketManager();
                socketManager.Start();
            }
            socketManager.Set(baseDevice);
        }
    }
}